package net.zabuchy.customer.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.address.entity.Address;
import net.zabuchy.customer.bo.CustomerBo;
import net.zabuchy.customer.entity.Customer;
import net.zabuchy.customer.entity.LicenceDetail;
import net.zabuchy.helper.AddressHelper;
import net.zabuchy.helper.EmailHelper;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.user.action.CreateUserAction;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


public class CreateCustomerAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {
	
	@Autowired
	private SessionHelper sessionBean;
	@Autowired
	private CustomerBo customerBo;
	@Autowired
	private EmailHelper emailHelper;
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private static final Logger LOGGER = Logger.getLogger(CreateCustomerAction.class.getName());
	
	private int customerId;
	private Address address;
	private String companyName;
	private Date creationDate;
	private String director;
	private String email;
	private String fax;
	private String phone;
	private LicenceDetail licenceDetail;
	
	
	/**********************************************/
	private Date licenceExpired;
	private Date licenceGained;
	private int licenceNr;
	private int projectsLimit;
	private int usersLimit;
	private Customer customer;
	/**********************************************/
	private String city;
	private String country;
	private String address1;
	private String state;
	private String zipcode;
	private String[] countries = AddressHelper.getCountries();
	/**********************************************/
	
	public String execute() {
		
		LOGGER.debug("start");

		String Customer = request
				.getParameter(ParamNames.CREATE_NEW_CUSTOMER_FORM);

		boolean returnForm = (Customer != null ? Boolean
				.parseBoolean(Customer) : false);

		if (returnForm) {
			LOGGER.debug("clear all fields and return form");
			clearFields();
			return Action.INPUT;
		}

		if (customerBo == null) {
			return Action.ERROR;
		}

		creationDate = new Date(System.currentTimeMillis());
		
		Customer customer = new Customer(null, companyName, creationDate, director, email, fax, phone, null);
		
		customerBo.add(customer);
		this.customerId = customer.getCustomerId();
		LOGGER.debug("customer.getCustomerId(): " + customer.getCustomerId());
				
		//emailHelper.sendMail("dupa", email, "Welcome in our system!", "Hello!");
		
		return Action.SUCCESS;
		
	}
	
	
	
	public String addCustomerDetails() {
		LOGGER.debug("customerId: " + customerId);
		
		LicenceDetail details = new LicenceDetail();
		customer = customerBo.findById(customerId);
		details.setCustomer(customer);
		details.setLicenceExpired(licenceExpired);
		details.setLicenceGained(licenceGained);
		details.setLicenceNr(licenceNr);
		details.setProjectsLimit(projectsLimit);
		details.setUsersLimit(usersLimit);
		customer.setLicenceDetail(details);
		customerBo.update(customer);
		
		return Action.SUCCESS;
	}

	public String addCustomerAddress() {
		LOGGER.debug("customerId: " + customerId);
		LOGGER.debug("licenceExpired: " + String.valueOf(licenceExpired));
		LOGGER.debug("licenceGained: " + String.valueOf(licenceGained));
		Address address = new Address();
		address.setCity(city);
		address.setCountry(country);
		address.setState(state);
		address.setZipcode(zipcode);
		address.setAddress1(address1);
		
		customer = customerBo.findById(customerId);
		customer.setAddress(address);
		customerBo.update(customer);
		
		return Action.SUCCESS;
	}
	
	
	
	private void clearFields() {
		LOGGER.debug("TO DO");
		companyName = null;
		creationDate = null;
		director = null;
		email = null;
		fax = null;
		phone = null;
		licenceDetail = null;
		
		
		/**********************************************//*
		private Date licenceExpired;
		private Date licenceGained;
		private int licenceNr;
		private int projectsLimit;
		private int usersLimit;
		private Customer customer;
		*//**********************************************//*
		private String city;
		private String country;
		private String address1;
		private String state;
		private String zipcode;*/
	}
	
	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public CustomerBo getCustomerBo() {
		return customerBo;
	}

	public void setCustomerBo(CustomerBo customerBo) {
		this.customerBo = customerBo;
	}

	public EmailHelper getEmailHelper() {
		return emailHelper;
	}

	public void setEmailHelper(EmailHelper emailHelper) {
		this.emailHelper = emailHelper;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public String getDirector() {
		return director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public LicenceDetail getLicenceDetail() {
		return licenceDetail;
	}

	public void setLicenceDetail(LicenceDetail licenceDetail) {
		this.licenceDetail = licenceDetail;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getServletRequest() {
		return request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpServletResponse getServletResponse() {
		return response;
	}

	public Date getLicenceExpired() {
		return licenceExpired;
	}

	public void setLicenceExpired(Date licenceExpired) {
		this.licenceExpired = licenceExpired;
	}

	public Date getLicenceGained() {
		return licenceGained;
	}

	public void setLicenceGained(Date licenceGained) {
		this.licenceGained = licenceGained;
	}

	public int getLicenceNr() {
		return licenceNr;
	}

	public void setLicenceNr(int licenceNr) {
		this.licenceNr = licenceNr;
	}

	public int getProjectsLimit() {
		return projectsLimit;
	}

	public void setProjectsLimit(int projectsLimit) {
		this.projectsLimit = projectsLimit;
	}

	public int getUsersLimit() {
		return usersLimit;
	}

	public void setUsersLimit(int usersLimit) {
		this.usersLimit = usersLimit;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}



	public String getCity() {
		return city;
	}



	public void setCity(String city) {
		this.city = city;
	}



	public String getCountry() {
		return country;
	}



	public void setCountry(String country) {
		this.country = country;
	}



	public String getAddress1() {
		return address1;
	}



	public void setAddress1(String address1) {
		this.address1 = address1;
	}



	public String getState() {
		return state;
	}



	public void setState(String state) {
		this.state = state;
	}



	public String getZipcode() {
		return zipcode;
	}



	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}



	public String[] getCountries() {
		return countries;
	}



	public void setCountries(String[] countries) {
		this.countries = countries;
	}
	
}